/*
 * @Author: ztao
 * @Date: 2022-04-12 18:47:27
 * @LastEditTime: 2022-04-15 19:58:59
 * @Description: 两数之和
 */

//解法一: 暴力解法
var twoSum1 = function (nums, target) {
  for (let i = 0; i < nums.length - 1; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      if (nums[i] + nums[j] === target) {
        return [i, j];
      }
    }
  }
};
//解法二: 哈希表解法
var twoSum2 = function (nums, target) {
  let map = new Map();
  map.set(nums[0], 0);
  for (let i = 1; i < nums.length; i++) {
    let diffVal = target - nums[i];
    if (map.has(diffVal)) {
      return [map.get(diffVal), i];
    }
    map.set(nums[i], i);
  }
  return null;
};

twoSum1([11, 2, 11, 15, 10, 9, 8, 7], 9);
